package matlap.client.view.admin;

import java.util.Iterator;
import java.util.List;

import matlap.client.services.article.ArticleService;
import matlap.client.services.article.ArticleServiceAsync;
import matlap.client.services.school.SchoolService;
import matlap.client.services.school.SchoolServiceAsync;
import matlap.shared.Article;
import matlap.shared.Author;
import matlap.shared.Topic;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ArticlesComposite extends Composite {

	private VerticalPanel newTopicPanel;
	private VerticalPanel newAuthorPanel;
	private VerticalPanel newArticlePanel;
	private FileUpload fileUpload;
	private HorizontalPanel absolutePanel;
	private VerticalPanel vp;
	private VerticalPanel gridPanel = new VerticalPanel();
	private Grid g;
	private Button btnUpload;
	ArticleServiceAsync c1 = GWT.create(ArticleService.class);
	SchoolServiceAsync s1 = GWT.create(SchoolService.class);
	private Label lbTopics;
	private ListBox cbTopics;
	private ListBox cbAuthors;
	private Label lbAuthors;
	private TextBox tbTitle;
	private TextBox tbNewAuthor;
	private TextBox tbPath;

	public ArticlesComposite() {

		vp = new VerticalPanel();
		initWidget(vp);
		absolutePanel = new HorizontalPanel();

		initNewArticlaPanel();
		absolutePanel.add(newArticlePanel);
		newArticlePanel.setHeight("238");
		initNewTopicPanel();
		initNewAuthorPanel();
		absolutePanel.add(newTopicPanel);
		newTopicPanel.setSize("", "238px");
		absolutePanel.add(newAuthorPanel);
		newAuthorPanel.setHeight("238px");

		vp.add(absolutePanel);

		initGridPanel();

		DecoratorPanel decoratorPanel = new DecoratorPanel();
		vp.add(decoratorPanel);

		Label lblNewLabel_1 = new Label("Felt\u00F6lt\u00F6tt cikkek");
		lblNewLabel_1.setStyleName("BigBold");
		decoratorPanel.setWidget(lblNewLabel_1);
		vp.add(gridPanel);

	}

	private void initGridPanel() {

		gridPanel.clear();

		g = new Grid(1, 3);
		Label lbSzerzo = new Label("Szerz\u0151");
		lbSzerzo.setStyleName("Header");
		g.setWidget(0, 0, lbSzerzo);
		
		Label lbCim = new Label("C\u00EDm");
		lbCim.setStyleName("Header");
		g.setWidget(0, 1, lbCim);
		
		Label lbTemakor = new Label("T\u00E9mak\u00F6r");
		lbTemakor.setStyleName("Header");
		g.setWidget(0, 2, lbTemakor);
		
		g.setBorderWidth(2);

		gridPanel.add(g);
		g.setSize("798px", "35px");

		c1.getArticlesNumber(new AsyncCallback<Integer>() {

			public void onFailure(Throwable caught) {
				Window.alert("Database problem in article 1");

			}

			public void onSuccess(Integer result) {
				g.resize(result + 1, 3);

			}

		});

		c1.selectArticle("", "", "", new AsyncCallback<List<Article>>() {

			public void onFailure(Throwable caught) {
				Window.alert("Database problem in article");

			}

			public void onSuccess(List<Article> result) {
				Iterator<Article> it = result.iterator();
				int i = 1;
				Article art;
				while (it.hasNext()) {
					art = (Article) it.next();
					g.setText(i, 0, art.getAuthor());
					g.setText(i, 1, art.getTitle());
					g.setText(i, 2, art.getTopic());
					i++;
				}

			}

		});

	}

	public void get_Doc() {
		btnUpload.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				int i = cbAuthors.getSelectedIndex();
				String author = cbAuthors.getItemText(i);

				i = cbTopics.getSelectedIndex();
				String topic = cbTopics.getItemText(i);

				String title = tbTitle.getText();
				String fileName = "";
				final String fullPath = fileUpload.getFilename();
				final int lastIndex = fullPath.lastIndexOf('\\');
				fileName = fullPath.substring(lastIndex + 1);
				String path = tbPath.getText();

				if (fileName.length() == 0) {
					Window.alert("Nem adt\u00E1l meg file-t!");
				} else if (path.length() == 0) {
					Window.alert("Nem adt\u00E1l meg \u00FAtvonalat!");
				} else if (title.length() == 0) {
					Window.alert("Nem adt\u00E1l meg c\u00EDmet!");
				} else
					c1.insertArticle(title, author, topic, fileName, path,
							new AsyncCallback<Boolean>() {

								@Override
								public void onSuccess(Boolean result) {
									if (result) {
										Window.alert("Cikk felt\u00F6ltve");
										cbTopics.setSelectedIndex(0);
										cbAuthors.setSelectedIndex(0);
										tbTitle.setText("");
										fileUpload.setName("");
										tbPath.setText("");
										initGridPanel();
									} else
										Window.alert("Ilyen című, szerzőjű, témakörű cikk már fel van töltve");

								}

								public void onFailure(Throwable caught) {
									Window.alert(caught.getMessage());
								}

							});

			}
		});

	}

	public void updateTopicComboBox() {
		cbTopics.clear();
		c1.selectTopicsName(new AsyncCallback<List<Topic>>() {

			public void onSuccess(List<Topic> result) {
				Iterator<Topic> it = result.iterator();
				while (it.hasNext()) {
					Topic top = (Topic) it.next();
					cbTopics.addItem(top.getTopicsName());
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Sikertelen");
			}
		});
	}

	public void updateAuthorComboBox() {
		cbAuthors.clear();
		c1.selectAuthorName(new AsyncCallback<List<Author>>() {

			public void onSuccess(List<Author> result) {
				Iterator<Author> it = result.iterator();
				while (it.hasNext()) {
					Author aut = (Author) it.next();
					cbAuthors.addItem(aut.getAuthorName());
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Database problem");
			}

		});

	}

	public void initNewArticlaPanel() {

		newArticlePanel = new VerticalPanel();
		newArticlePanel.setBorderWidth(1);
		DecoratorPanel decoratorPanel = new DecoratorPanel();
		newArticlePanel.add(decoratorPanel);
		newArticlePanel.setCellHorizontalAlignment(decoratorPanel,
				HasHorizontalAlignment.ALIGN_CENTER);
		Label lblNewLabel_2 = new Label("\u00DAj cikk felt\u00F6lt\u00E9se:");
		lblNewLabel_2.setStyleName("BigBold");
		decoratorPanel.setWidget(lblNewLabel_2);

		FlexTable flexTable = new FlexTable();
		newArticlePanel.add(flexTable);
		flexTable.setSize("302px", "100px");

		Label lblFile = new Label("File");
		flexTable.setWidget(0, 0, lblFile);
		lblFile.setSize("27px", "21px");
		fileUpload = new FileUpload();
		flexTable.setWidget(0, 1, fileUpload);

		Label lbPath = new Label("El\u00E9r\u00E9si \u00FAtvonal");
		flexTable.setWidget(1, 0, lbPath);

		tbPath = new TextBox();
		flexTable.setWidget(1, 1, tbPath);
		tbPath.setWidth("252px");

		Label lbTitle = new Label("C\u00EDm");
		flexTable.setWidget(2, 0, lbTitle);

		tbTitle = new TextBox();
		flexTable.setWidget(2, 1, tbTitle);
		tbTitle.setWidth("252px");

		lbAuthors = new Label("Szerz\u0151k");
		flexTable.setWidget(3, 0, lbAuthors);

		cbAuthors = new ListBox();
		cbAuthors.addItem(" ");
		flexTable.setWidget(3, 1, cbAuthors);

		tbNewAuthor = new TextBox();
		flexTable.setWidget(3, 2, tbNewAuthor);
		flexTable.getCellFormatter().setVisible(3, 2, false);
		updateAuthorComboBox();

		lbTopics = new Label("T\u00E9mak\u00F6r");
		flexTable.setWidget(4, 0, lbTopics);

		cbTopics = new ListBox();
		cbTopics.addItem(" ");
		flexTable.setWidget(4, 1, cbTopics);
		updateTopicComboBox();

		btnUpload = new Button("");
		flexTable.setWidget(5, 1, btnUpload);

		btnUpload.setText("Felt\u00F6lt\u00E9s");
		btnUpload.setSize("133px", "28px");
		flexTable.getCellFormatter().setHorizontalAlignment(5, 1,
				HasHorizontalAlignment.ALIGN_CENTER);
		get_Doc();

	}

	public void initNewTopicPanel() {
		newTopicPanel = new VerticalPanel();
		newTopicPanel.setBorderWidth(1);
		DecoratorPanel decoratorPanel = new DecoratorPanel();
		newTopicPanel.add(decoratorPanel);
		newTopicPanel.setCellHorizontalAlignment(decoratorPanel,
				HasHorizontalAlignment.ALIGN_CENTER);
		Label lblNewLabel_2 = new Label(
				"\u00DAj t\u00E9mak\u00F6r felvezet\u00E9se:");
		lblNewLabel_2.setStyleName("BigBold");
		decoratorPanel.setWidget(lblNewLabel_2);

		FlexTable fl = new FlexTable();
		newTopicPanel.add(fl);
		fl.setHeight("191px");

		Label vnLabel = new Label("T\u00E9mak\u00F6r neve:");
		fl.setWidget(0, 0, vnLabel);
		vnLabel.setWidth("225px");

		final TextBox nameBox = new TextBox();
		fl.setWidget(1, 0, nameBox);

		Label lblNewLabel = new Label((String) null);
		fl.setWidget(2, 0, lblNewLabel);

		Button btnNewButton_1 = new Button("New button");
		btnNewButton_1.setText("Ment\u00E9s");
		fl.setWidget(3, 0, btnNewButton_1);
		fl.getCellFormatter().setHorizontalAlignment(3, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		fl.getCellFormatter().setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		fl.getCellFormatter().setHorizontalAlignment(1, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		btnNewButton_1.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String name = nameBox.getText();
				c1.insertTopic(name, new AsyncCallback<Void>() {

					public void onFailure(Throwable caught) {
						Window.alert("Database problem");

					}

					public void onSuccess(Void result) {
						Window.alert("Sikeres");
						nameBox.setText("");
						updateTopicComboBox();

					}

				});
			}

		});

	}

	public void initNewAuthorPanel() {
		newAuthorPanel = new VerticalPanel();
		newAuthorPanel.setBorderWidth(1);
		DecoratorPanel decoratorPanel = new DecoratorPanel();
		newAuthorPanel.add(decoratorPanel);
		newAuthorPanel.setCellHorizontalAlignment(decoratorPanel,
				HasHorizontalAlignment.ALIGN_CENTER);
		Label lblNewLabel_2 = new Label("\u00DAj szerz\u0151 felvezet\u00E9se:");
		lblNewLabel_2
				.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		lblNewLabel_2.setStyleName("BigBold");
		decoratorPanel.setWidget(lblNewLabel_2);

		FlexTable fl = new FlexTable();
		newAuthorPanel.add(fl);
		fl.setHeight("191px");

		Label vnLabel = new Label("Szerz\u0151 neve");
		vnLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		fl.setWidget(0, 0, vnLabel);
		vnLabel.setWidth("225px");

		final TextBox nameBox = new TextBox();
		fl.setWidget(1, 0, nameBox);

		Label nameLabel = new Label("Helys\u00E9g");
		fl.setWidget(2, 0, nameLabel);

		final ListBox localityBox = new ListBox();
		localityBox.addItem("");
		fl.setWidget(3, 0, localityBox);
		s1.selectLocality(new AsyncCallback<List<String>>() {

			public void onFailure(Throwable caught) {
				Window.alert("Error");
			}

			public void onSuccess(List<String> result) {
				Iterator<String> it = result.iterator();
				while (it.hasNext()) {
					String str = (String) it.next();
					localityBox.addItem(str);
				}

			}

		});

		Button btnNewButton = new Button("New button");
		btnNewButton.setText("Ment\u00E9s");
		btnNewButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				String name = nameBox.getText();
				String loc = localityBox.getItemText(localityBox
						.getSelectedIndex());

				if ((name.length() == 0) || (loc.length() == 0)) {
					Window.alert("Nem adott meg minden adatot!");
				} else {
					c1.insertAuthor(name, loc, new AsyncCallback<Void>() {

						public void onFailure(Throwable caught) {
							Window.alert("Error");
						}

						public void onSuccess(Void result) {
							Window.alert("Sikeres");
						}

					});
					nameBox.setText("");
					localityBox.setSelectedIndex(0);
					updateAuthorComboBox();
				}
			}

		});
		fl.setWidget(4, 0, btnNewButton);
		fl.getCellFormatter().setHorizontalAlignment(1, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		fl.getCellFormatter().setHorizontalAlignment(2, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		fl.getCellFormatter().setHorizontalAlignment(3, 0,
				HasHorizontalAlignment.ALIGN_CENTER);
		fl.getCellFormatter().setHorizontalAlignment(4, 0,
				HasHorizontalAlignment.ALIGN_CENTER);

	}
}
